import { type MaybeRef, computed, isRef } from 'vue'
import { useQuery } from '@tanstack/vue-query'
import type { SearchUserQueryKey } from './types'
import { ApiSearchUser } from '.'

export const SearchUserKeyPrefix: [SearchUserQueryKey[0], SearchUserQueryKey[1]] = ['get', 'users']
export function useSearchUserQueryKey(
  query: MaybeRef<SearchUserQueryKey[2]>
): MaybeRef<SearchUserQueryKey> {
  return isRef(query) ? computed(() => ['get', 'users', query.value]) : ['get', 'users', query]
}
export function useSearchUser(query: MaybeRef<SearchUserQueryKey[2]>) {
  return useQuery({
    staleTime: 60_000,
    queryKey: useSearchUserQueryKey(query),
    queryFn: async ({ queryKey: [, , query] }) => ApiSearchUser(query),
    select: ({ data }) => data
  })
}
